package modellayer;

import java.util.ArrayList;
import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;

/**
 * The Class DVD.
 */
public class DVD {

	/** The barcode. */
	private String barcode;

	/** The title. */
	private String title;

	/** The publication date. */
	private Date publicationDate;

	/** The date format. */
	private SimpleDateFormat dateFormat;

	/** The copies. */
	private ArrayList<Copy> copies;

	/**
	 * Instantiates a new dvd.
	 *
	 * @param title the title
	 * @param barcode the barcode
	 * @param publicationDate the publication date
	 */
	public DVD(String title, String barcode, String publicationDate) 
	{
		this.barcode = barcode;
		this.title = title;
		copies = new ArrayList<Copy>();


		dateFormat = new SimpleDateFormat("dd/MM/yyyy");		
		//Try-Parse String input to Date object
		try
		{
			this.publicationDate = dateFormat.parse(publicationDate);
		}
		catch (ParseException e)
		{
			this.publicationDate = null;
			System.out.println("Date not typed correctly!");
		}
	}

	/**
	 * Creates the copy.
	 *
	 * @param serial the serial
	 * @param purchaseDate the purchase date
	 * @param purchasePrice the purchase price
	 * @return the string
	 */
	public String createCopy(String title, String serialNumber, String purchaseDate, String purchasePrice)
	{
		if(findCopy(serialNumber) != null){
			return "There is already a copy with that serial!";
		}
		else{
			// Creation of a new Copy, from values returned from the UI
			Copy c = new Copy(title, serialNumber, purchaseDate, purchasePrice);
			// The copy is added to the collection of copies
			copies.add(c);
			return "Copy created";
		}
	}

	/**
	 * Gets the barcode.
	 *
	 * @return the barcode
	 */
	public String getBarcode() 
	{
		return barcode;
	}

	/**
	 * Sets the barcode.
	 *
	 * @param barcode the barcode to set
	 */
	public void setBarcode(String barcode) 
	{
		this.barcode = barcode;
	}

	/**
	 * Gets the title.
	 *
	 * @return the title
	 */
	public String getTitle() 
	{
		return title;
	}

	/**
	 * Sets the title.
	 *
	 * @param title the title to set
	 */
	public void setTitle(String title) 
	{
		this.title = title;
	}

	/**
	 * Gets the publication date.
	 *
	 * @return the publicationDate
	 */
	public String getPublicationDate() 
	{
		if(publicationDate == null)
		{
			return null;
		}
		else{
			return dateFormat.format(publicationDate);
		}

	}

	/**
	 * Sets the publication date.
	 *
	 * @param publicationDate the publicationDate to set
	 */
	public void setPublicationDate(String publicationDate) 
	{
		try
		{
			this.publicationDate = dateFormat.parse(publicationDate);
		}
		catch (ParseException e)
		{
			e.printStackTrace();
		}
	}

	/**
	 * Find copy.
	 *
	 * @param serialNumber the serial number
	 * @return the copy
	 */
	public Copy findCopy(String serialNumber)
	{
		int i=0;
		boolean found = false;
		while (!found && i < copies.size()) {       
			if (serialNumber.equals(copies.get(i).getSerialNumber())){
				found = true;
			}
			else i++;
		}
		if (found)
		{
			return copies.get(i);
		}
		else 
			return null;
	}

	/**
	 * Delete copy.
	 *
	 * @param c the c
	 */
	public void deleteCopy(Copy c){
		copies.remove(c);
	}
	public Copy getHomeCopy(){
		int i=0;
		Copy c = null;
		boolean found = false;
		while (!found && i < copies.size()) {       
			if (copies.get(i).isHome() == true){
				c = copies.get(i);
				found = true;
			}
			else i++;
		}
		if (found)
		{
			System.out.println(c.getTitle());
			return c;
		}
		else 
			return null;
	}

	/**
	 * Gets the copies.
	 *
	 * @return the copies
	 */
	public ArrayList<Copy> getCopies()
	{
		return copies;
	}
}
